#include <iostream>
#include <memory>
using std::unique_ptr;
using std::cout;
using std::endl;

class Component{
public:
    virtual void operation()=0;
};

class ConcretComponent:public Component{
public:
    void operation() override{
        cout << "最初要执行的内容" << endl;
    }
};

class Decorator:public Component{
public:
    Decorator(): _pre(nullptr){}

    void set_pre(Component * pre){
        _pre = pre;
    }
    virtual void operation() = 0;
protected:
    Component * _pre;
};

class Decorator_a: public Decorator{
public:
    void add_dec(){
        cout << "add Decorator a" << endl;
    }
    void operation () override{
        if(_pre){
            _pre->operation();
            add_dec();
        }
    }
};
class Decorator_b: public Decorator{
public:
    void add_dec(){
        cout << "add Decorator b" << endl;
    }
    void operation () override{
        if(_pre){
            _pre->operation();
            add_dec();
        }
    }
};


void test(){
    unique_ptr<Component> concret(new ConcretComponent());
    unique_ptr<Decorator> dec_a(new Decorator_a());
    unique_ptr<Decorator> dec_b(new Decorator_b());

    dec_b->set_pre(dec_a.get());
    dec_a->set_pre(concret.get());

    dec_b->operation();
}

int main()
{


    test ();
    return 0;
}

